package org.daidai.abstrusewriting.homepage.live.networks;

import android.util.Log;

import com.google.gson.Gson;

import org.daidai.abstrusewriting.homepage.live.models.AlbumId;
import org.daidai.abstrusewriting.homepage.live.models.AlbumIdListItem;
import org.daidai.abstrusewriting.homepage.live.models.AlbumItem;
import org.daidai.abstrusewriting.homepage.live.models.DetailHeader;
import org.daidai.abstrusewriting.homepage.live.models.liveItem;
import org.daidai.abstrusewriting.homepage.live.models.liveSmiple;
import org.greenrobot.eventbus.EventBus;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class ClientNetwork {
    private static AbstactHttpUtils https;
   static
   {
       https=new okHttp();
   }

    public static void doLiveMain()
    {
        String url="http://mobile.ximalaya.com/m/explore_user_index?device=android&page=1";
        https.doGetDataAsync(url, new HttpTools() {
            @Override
            public void onSuccess(byte[] data) {
                if (data != null) {
                    String s = new String(data);
                    try {
                        JSONObject jsonObject = new JSONObject(s);

                        JSONArray list = jsonObject.optJSONArray("list");
                        Log.d("fffffffffffff", "onSuccess: "+list.length());
                        List<liveItem> liveItems =new ArrayList<liveItem>();
                        liveItem liveItem=null;
                        Gson gson = new Gson();
                        for (int i = 0; i < list.length(); i++) {
                            JSONObject jot = list.optJSONObject(i);
                            liveItem = new liveItem();
                            liveItem.setId(jot.optLong("id"));
                            liveItem.setName(jot.optString("name"));
                            liveItem.setTitle(jot.optString("title"));
                            JSONArray array = jot.optJSONArray("list");
                            List<liveSmiple> livesmiples = new ArrayList<liveSmiple>();
                            for (int j = 0; j < array.length(); j++) {
                                JSONObject object = array.optJSONObject(j);
                                String str= object.toString();
                                Log.d("fffffffffffff", "onSuccess: "+str);
                                liveSmiple smiple = gson.fromJson(str, liveSmiple.class);
                                livesmiples.add(smiple);
                                Log.d("fffffffffffff", "onSuccess: "+livesmiples.size());
                            }
                            liveItem.setList(livesmiples);
                            liveItems.add(liveItem);
                            Log.d("rrrrrrr", "onSuccess: "+liveItems.get(0).getName());
                            Log.d("rrrrrrr", "onSuccess: "+liveItems.size());

                        }


                        EventBus.getDefault().post(liveItems);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }
    public static void getData(String uid)
    {

        String url="http://mobile.ximalaya.com/mobile/others/ca/homePage?device=android&statEvent=pageview%2Fuser%4044841033&statModule=%E7%83%AD%E9%97%A8&statPage=tab%40%E5%8F%91%E7%8E%B0_%E4%B8%BB%E6%92%AD&statPosition=1&toUid="+uid;
        https.doGetDataAsync(url, new HttpTools() {
            @Override
            public void onSuccess(byte[] data) {
                if (data != null) {
                    String str = new String(data);
                    Gson gson = new Gson();
                    DetailHeader detailHeader = gson.fromJson(str, DetailHeader.class);
                    Log.d("ggggggggggggg", "jjj: "+detailHeader.getBackgroundLogo());
                    Log.d("ggggggggggggg", "jj: "+detailHeader.getFollowings());
                    EventBus.getDefault().post(detailHeader);
                }
            }
        });
    }
    public static void getAbomData(String uid)
    {
     String url =" http://mobile.ximalaya.com/mobile/v1/artist/albums?toUid="+uid;
        https.doGetDataAsync(url, new HttpTools() {
            @Override
            public void onSuccess(byte[] data) {
                String s = new String(data);
                try {
                    JSONObject jsonObject = new JSONObject(s);
                    JSONArray array = jsonObject.optJSONArray("list");
                    Gson gson = new Gson();
                    AlbumId albumId=null;
                    for (int i = 0; i < array.length(); i++) {
                        JSONObject optJSONObject = array.optJSONObject(i);
                         String str = optJSONObject.toString();
                         albumId=gson.fromJson(str, AlbumId.class);
                    }
                    Log.d("bbbbbbbbbbbbbbb", "onSuccess: "+albumId.getAlbumId());
                    EventBus.getDefault().post(albumId);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

    }
    public static void getAbulmItemDetail(String albumid)
    {
        String url ="http://mobile.ximalaya.com/mobile/v1/album?albumId="+albumid+"&device=android&pageId=null&pageSize=20&source=0";
        https.doGetDataAsync(url, new HttpTools() {
            @Override
            public void onSuccess(byte[] data) {
                if (data != null) {
                    String str = new String(data);
                    Gson gson = new Gson();
                    try {
                        JSONObject json = new JSONObject(str);
                        JSONObject jsondata = json.optJSONObject("data");
                        JSONObject tracks = jsondata.optJSONObject("tracks");
                        JSONArray list = tracks.optJSONArray("list");
                        ArrayList<AlbumIdListItem> albumIdListItems = new ArrayList<>();
                        int length = list.length();
                        for (int i = 0; i < length; i++) {
                            JSONObject object = list.optJSONObject(i);
                            String s = object.toString();
                            AlbumIdListItem albumIdListItem = gson.fromJson(s, AlbumIdListItem.class);
                            albumIdListItems.add(albumIdListItem);
                        }
                        EventBus.getDefault().post(albumIdListItems);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }


            }
        });
    }
}
